package com.epk.pic.controller;

import com.epk.pic.controller.base.BaseController;
import com.epk.pic.mapper.PicMapper;
import com.epk.pic.mapper.TargetMapper;
import com.epk.pic.po.PicContents;
import com.epk.pic.po.PicInfoMain;
import com.epk.pic.po.PicTargetMain;
import com.epk.pic.po.PicTargets;
import com.epk.pic.service.PicService;
import com.epk.pic.vo.PicInfoVo;
import com.epk.pic.vo.PicQueryVo;
import com.epk.pic.vo.base.ApiResponse;
import com.epk.pic.vo.base.TableDataInfo;
import com.github.pagehelper.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;


@RestController
@RequestMapping("/pic")
public class PicController extends BaseController {
    @Autowired
    private PicMapper picMapper;
    @Autowired
    private PicService picService;

    @PostMapping("/query")
    public TableDataInfo<PicContents> query(@RequestBody PicQueryVo queryVo) throws IOException {
        Integer searchSub = queryVo.getSearchSub();
        searchSub = searchSub == null || searchSub == 0 ? 1 : searchSub < 0 ? 999 : searchSub;
        List<String> targetGroup = picService.getTargetGroup(queryVo.getTargetsMap(), searchSub);
        queryVo.setTargetGroup(targetGroup);
        Page<?> objects = super.startPage(queryVo);
        List<PicContents> picContents = picMapper.selectPicByQueryVo(queryVo);
        return TableDataInfo.build(objects, picContents);
    }

    @GetMapping({"/v/{tagValue}/{pageNo}", "/v/{tagValue}"})
    @Transactional
    public TableDataInfo<PicContents> simpleV(@PathVariable String tagValue, @PathVariable(required = false) Integer pageNo) throws IOException {
        return this.simpleT("other", tagValue, pageNo);
    }

    @GetMapping({"/t/{tagName}/{tagValue}/{pageNo}", "/t/{tagName}/{tagValue}"})
    @Transactional
    public TableDataInfo<PicContents> simpleT(@PathVariable String tagName, @PathVariable String tagValue, @PathVariable(required = false) Integer pageNo) throws IOException {
        PicQueryVo picQueryVo = new PicQueryVo();
        picQueryVo.setPageNo(pageNo);
        Map<String, String> stringStringMap = new HashMap<>();
        stringStringMap.put(tagName, tagValue);
        picQueryVo.setTargetsRelate("or");
        picQueryVo.setTargetsMap(stringStringMap);
        return this.query(picQueryVo);
    }

    @GetMapping({"/s/{title}/{pageNo}", "/s/{title}"})
    @Transactional
    public TableDataInfo<PicContents> simpleS(@PathVariable String title, @PathVariable(required = false) Integer pageNo) throws IOException {
        PicQueryVo picQueryVo = new PicQueryVo();
        picQueryVo.setPageNo(pageNo);
        picQueryVo.setTitle(title);
        return this.query(picQueryVo);
    }

    @GetMapping({"/a/{auth}/{pageNo}", "/a/{auth}"})
    @Transactional
    public TableDataInfo<PicContents> simpleA(@PathVariable String auth, @PathVariable(required = false) Integer pageNo) throws IOException {
        PicQueryVo picQueryVo = new PicQueryVo();
        picQueryVo.setPageNo(pageNo);
        picQueryVo.setAuth(auth);
        return this.query(picQueryVo);
    }

}